package simbot.example.shop;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import simbot.example.entity.ItemsJiu;
import simbot.example.mapper.ItemsJiuMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * CYC
 **/
@Service
public class BuyItem {
    @Resource
    private ItemsJiuMapper itemsJiuMapper;

    public void buy(String itemName,String accountCode){
        List<ItemsJiu> itemsJius = itemsJiuMapper.selectList(Wrappers.<ItemsJiu>lambdaQuery().eq(ItemsJiu::getUsername, accountCode).eq(ItemsJiu::getItemname,itemName));
        if (itemsJius.size()<1){
            ItemsJiu itemsJiu = new ItemsJiu();
            itemsJiu.setUsername(accountCode);
            itemsJiu.setItemname(itemName);
            itemsJiu.setQuantity(1);
            itemsJiuMapper.insert(itemsJiu);
        }else {
            itemsJius.get(0).setQuantity(itemsJius.get(0).getQuantity()+1);
            itemsJiuMapper.update(itemsJius.get(0),Wrappers.<ItemsJiu>lambdaQuery().eq(ItemsJiu::getUsername, accountCode).eq(ItemsJiu::getItemname,itemName));
        }

    }
}
